<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29 0029
 * Time: 上午 9:12
 */

namespace app\admin\model;


use app\admin\model\Base;
use think\Db;

class Admin extends Base
{

    protected $table = "admin";

    protected $base_arr = [
            'table' => 'admin',
            'field' => 'admin_name,roleid,is_deleted,depart_id,name',
            'primary_key' => 'admin_id'
            ];


    //附属的表单
    protected $attach_arr =  [
            [
            'table' => 'authority_role',
            'field' => 'rolename',
            'primary_key' => 'id',
            'base_table_field' => 'roleid'
            ],
        [
            'table' => 'doctor_department',
            'field' => 'department',
            'primary_key' => 'id',
            'base_table_field' => 'depart_id'
        ],

            ];

    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql(){
        $data = [
            't_name' => 'admin',
            'model' => '管理员',
            'admin_name' => '用户名',
            'name' => '姓名',
            'roleid' => '角色名称',
            'depart_id' => '部门',
            'is_deleted' => '{"1":"功能启用","2":"功能禁用","3":"功能删除"}'
        ];
        return $data;
    }


    /**
     * 获取日志的访问方法
     * @param $list_action 当前访问所要跳转的路径
     * @param $data form表单提交过来的数据
     **/
    public static function addAdminAdminLog($list_action,$data=[],$userId,$admin_type)
    {

        $model = new Admin();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];
        $type = '';

        switch($actionName){
            //列表
            case "admin_list":
                Base::save_admin_log($userId,$model,'check');
                break;
            case "add_admin":
                $type = 'add';
                if ($model->request->isPost()) {
                    //  $role_ids = implode(',',$data['role_ids']);
                    // $data['roleid'] = $role_ids;
                    $data['roleid'] = $data['role_ids'];
                    unset($data['role_ids']);
                    $data['admin_password'] = md5($data['admin_password']);
                }
                break;
            case "edit_admin":
                $type = 'edit';
                if ($model->request->isPost()) {
                    $data['roleid'] = $data['role_ids'];
                    unset($data['role_ids']);
                    if($data['admin_password']){
                        $data['admin_password'] = md5($data['admin_password']);
                    }else{
                        unset($data['admin_password']);
                    }

                    $old_data = Base::getOneOldDataByTables($data['id'],$model->base_arr,$model->attach_arr);

                }

                break;
            case "del_admin":
                $type = 'del';
                if ($model->request->isPost()) {
                    $id = $data["id"];
                    Db::execute("update admin set is_deleted=3  where admin_id=?", [ $id]);

                }
                break;
        }

        if($model->request->isPost()){

            //$model 实例化之后的模型
            //$type 数据的操作类型
            //$data  form表单提交之后的表单
            //$url 跳转路径
            //$old_data 原有的数据
            //$attach_arr 附属表单数组
            //$primary_key 基本表单的主键值id
            $result =  Base::operaTable($model,$userId,$type,$data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'admin_id',$admin_type);

          //  $result =  Base::operaTable($model,$type,$data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'admin_id');
            return $result;
        }

        return true;


    }



}
